<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>我的订单 - 百货购物平台</title>
    <link rel="stylesheet" href="layui-v2.9.20/layui/css/layui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
        }

        body {
            background-color: #fafafa;
        }

        /* 顶部导航栏 - 全新粉色风格 */
        .header-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: linear-gradient(135deg, #ffecef 0%, #fff0f3 100%);
            padding: 0 40px;
            box-shadow: 0 4px 20px rgba(255, 156, 176, 0.15);
            position: relative;
            z-index: 1000;
            height: 70px;
            border-bottom: 1px solid #ffdde4;
        }

        .logo {
            display: flex;
            align-items: center;
            font-size: 28px;
            font-weight: 800;
            color: #ff4d7a;
            text-shadow: 1px 1px 3px rgba(255, 77, 122, 0.2);
        }

        .logo i {
            margin-right: 10px;
            font-size: 32px;
        }

        .nav-menu {
            display: flex;
            list-style: none;
            height: 100%;
        }

        .nav-menu li {
            display: flex;
            align-items: center;
            position: relative;
            padding: 0 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            height: 100%;
        }

        .nav-menu li a {
            text-decoration: none;
            color: #ff4d7a;
            font-size: 16px;
            font-weight: 600;
            display: flex;
            align-items: center;
            height: 100%;
            padding: 0 5px;
            transition: all 0.3s;
        }

        .nav-menu li a i {
            margin-right: 8px;
            font-size: 18px;
        }

        .nav-menu li:hover {
            background: rgba(255, 255, 255, 0.5);
        }

        .nav-menu li.active {
            background: linear-gradient(to bottom, rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.4));
        }

        .nav-menu li.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(to right, #ff8da1, #ff4d7a);
            border-radius: 3px 3px 0 0;
        }

        .nav-menu li.active a {
            color: #e6004c;
        }

        /* 搜索框样式 - 已优化 */
        .search-container {
            background-color: #fff;
            padding: 0;
            position: sticky;
            top: 0;
            z-index: 999;
            box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05);
            border-bottom: 1px solid #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 90px;
        }

        .search-box {
            width: 700px;
            margin: 0 auto;
            display: flex;
            border: 2px solid #ff4d7a;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(255, 77, 122, 0.2);
            height: 52px;
        }

        .search-select {
            padding: 0 15px;
            background: #fff;
            border: none;
            border-right: 1px solid #eee;
            outline: none;
            color: #ff4d7a;
            font-weight: 500;
            width: 130px;
            appearance: none;
            background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23ff4d7a' d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 10px center;
            background-size: 16px;
            cursor: pointer;
        }

        .search-select:focus {
            background-color: #fff9fb;
        }

        .search-input-container {
            flex: 1;
            display: flex;
            align-items: center;
            position: relative;
        }

        .search-input {
            flex: 1;
            border: none;
            padding: 0 20px 0 45px;
            font-size: 16px;
            outline: none;
            color: #333;
            height: 100%;
        }

        .search-icon {
            position: absolute;
            left: 15px;
            color: #999;
            font-size: 18px;
        }

        .search-button {
            background: linear-gradient(135deg, #ff4d7a 0%, #ff8da1 100%);
            color: white;
            border: none;
            padding: 0 35px;
            font-size: 17px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .search-button:hover {
            background: linear-gradient(135deg, #ff3366 0%, #ff7799 100%);
        }

        /* 内容容器 */
        .content-container {
            max-width: 1200px;
            margin: 40px auto;
            padding: 0 20px;
        }

        .section-title {
            position: relative;
            font-size: 26px;
            margin: 35px 0 25px;
            padding-left: 15px;
            color: #ff4d7a;
            font-weight: 700;
        }

        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 8px;
            bottom: 8px;
            width: 5px;
            background: linear-gradient(to bottom, #ff8da1, #ff4d7a);
            border-radius: 5px;
        }

        /* 订单列表样式 */
        .order-container {
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 5px 25px rgba(255, 141, 161, 0.15);
            padding: 25px;
            margin-bottom: 30px;
        }

        .order-item {
            border: 1px solid #f0f0f0;
            border-radius: 10px;
            margin-bottom: 20px;
            overflow: hidden;
        }

        .order-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 20px;
            background: #fff9fb;
            border-bottom: 1px solid #f0f0f0;
        }

        .order-info {
            display: flex;
        }

        .order-info div {
            margin-right: 30px;
        }

        .order-id {
            font-weight: 600;
            color: #ff4d7a;
        }

        .order-status {
            padding: 5px 15px;
            border-radius: 20px;
            background: #ffedf0;
            color: #ff4d7a;
            font-weight: 600;
        }

        .order-content {
            padding: 20px;
        }

        .order-product {
            display: flex;
            padding: 15px 0;
            border-bottom: 1px solid #f9f9f9;
        }

        .order-product:last-child {
            border-bottom: none;
        }

        .order-product img {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 8px;
            margin-right: 20px;
        }

        .order-product-info {
            flex: 1;
        }

        .order-product-title {
            font-size: 16px;
            color: #333;
            margin-bottom: 10px;
        }

        .order-product-spec {
            font-size: 14px;
            color: #888;
            margin-bottom: 10px;
        }

        .order-product-price {
            color: #ff4d7a;
            font-weight: 600;
        }

        .order-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 20px;
            background: #fafafa;
            border-top: 1px solid #f0f0f0;
        }

        .order-total {
            font-size: 18px;
            font-weight: 600;
        }

        .order-total span {
            color: #ff4d7a;
            font-size: 22px;
        }

        .order-actions button {
            padding: 8px 20px;
            margin-left: 10px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-view {
            background: #fff;
            border: 1px solid #ff4d7a;
            color: #ff4d7a;
        }

        .btn-view:hover {
            background: #fff9fb;
        }

        .btn-buy {
            background: linear-gradient(135deg, #ff4d7a 0%, #ff8da1 100%);
            border: none;
            color: white;
        }

        .btn-buy:hover {
            background: linear-gradient(135deg, #ff3366 0%, #ff7799 100%);
        }

        .btn-delete {
            background: #f8f8f8;
            border: 1px solid #e0e0e0;
            color: #666;
        }

        .btn-delete:hover {
            background: #f0f0f0;
        }

        /* 页脚样式 */
        .footer {
            background: linear-gradient(135deg, #343a40 0%, #23272b 100%);
            color: #fff;
            padding: 50px 0 30px;
            margin-top: 70px;
        }

        .footer-content {
            max-width: 1200px;
            margin: 0 auto;
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 35px;
            padding: 0 20px;
        }

        .footer-column h3 {
            font-size: 20px;
            margin-bottom: 22px;
            padding-bottom: 12px;
            border-bottom: 2px solid #ff4d7a;
            color: #fff;
        }

        .footer-column ul {
            list-style: none;
        }

        .footer-column li {
            margin-bottom: 14px;
        }

        .footer-column a {
            color: #ddd;
            text-decoration: none;
            transition: all 0.3s;
            display: flex;
            align-items: center;
        }

        .footer-column a i {
            margin-right: 8px;
            color: #ff8da1;
        }

        .footer-column a:hover {
            color: #ff8da1;
            transform: translateX(5px);
        }

        .copyright {
            text-align: center;
            padding-top: 40px;
            margin-top: 40px;
            border-top: 1px solid #444;
            color: #aaa;
            font-size: 14px;
            max-width: 1200px;
            margin: 40px auto 0;
        }

        /* 响应式调整 */
        @media (max-width: 992px) {
            .footer-content {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        @media (max-width: 768px) {
            .header-container {
                flex-direction: column;
                height: auto;
                padding: 15px;
            }

            .nav-menu {
                width: 100%;
                margin-top: 15px;
                justify-content: center;
            }

            .nav-menu li {
                padding: 10px 15px;
            }

            .search-box {
                width: 90%;
            }

            .order-header {
                flex-direction: column;
                align-items: flex-start;
            }

            .order-info {
                flex-wrap: wrap;
                margin-bottom: 10px;
            }

            .order-info div {
                margin-bottom: 10px;
            }

            .order-footer {
                flex-direction: column;
                align-items: flex-start;
            }

            .order-total {
                margin-bottom: 15px;
            }

            .order-actions {
                display: flex;
                flex-wrap: wrap;
            }

            .order-actions button {
                margin-bottom: 10px;
            }
        }

        @media (max-width: 576px) {
            .nav-menu {
                flex-wrap: wrap;
            }

            .nav-menu li {
                padding: 8px 12px;
            }

            .search-box {
                flex-direction: column;
                border-radius: 15px;
                height: auto;
            }

            .search-select {
                width: 100%;
                padding: 12px 20px;
                border-right: none;
                border-bottom: 1px solid #eee;
                height: 50px;
            }

            .search-input-container {
                width: 100%;
            }

            .search-input {
                padding: 12px 20px 12px 45px;
                height: 50px;
            }

            .search-icon {
                left: 20px;
            }

            .search-button {
                padding: 15px;
                height: 50px;
            }

            .footer-content {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>

<body>
    <!-- 顶部导航栏 -->
    <div class="header-container">
        <div class="logo">
            <i class="fas fa-shopping-bag"></i>
            <span>百货购物平台</span>
        </div>
        <ul class="nav-menu">
            <li>
                <a href="index.html"><i class="fas fa-home"></i> 首页</a>
            </li>
            <li>
                <a href="cart item.html"><i class="fas fa-shopping-cart"></i> 购物车</a>
            </li>
            <li>
                <a href="checkout.html"><i class="fas fa-cash-register"></i> 结算</a>
            </li>
            <li class="active">
                <a href="order.html"><i class="fas fa-file-invoice"></i> 我的订单</a>
            </li>
            <li>
                <a href="person_center.html"><i class="fas fa-user-circle"></i> 个人中心</a>
            </li>
            <li>
                <a href="customer_service.html"><i class="fas fa-headset"></i> 官方客服</a>
            </li>
        </ul>
    </div>

    <!-- 搜索框 -->
    <div class="search-container">
        <div class="search-box">
            <select class="search-select">
                <option value="" disabled selected>全部商品</option>
                <option value="AAA">女装</option>
                <option value="BBB">男装</option>
                <option value="CCC">美妆</option>
                <option value="DDD">数码</option>
                <option value="EEE">食品</option>
            </select>
            <div class="search-input-container">
                <i class="fas fa-search search-icon"></i>
                <input type="text" class="search-input" placeholder="输入搜索关键字...">
            </div>
            <button class="search-button">搜索</button>
        </div>
    </div>

    <!-- 内容容器 -->
    <div class="content-container">
        <h2 class="section-title">我的订单</h2>

        <div class="order-container">
            <div class="order-item">
                <div class="order-header">
                    <div class="order-info">
                        <div class="order-id">订单号：20230621123456</div>
                        <div>下单时间：2023-06-21 10:25:36</div>
                    </div>
                </div>
                <div class="order-content">
                    <div class="order-product">
                        <img src="https://images.unsplash.com/photo-1596462502278-27bfdc403348?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=300&q=80"
                            alt="薇诺娜">
                        <div class="order-product-info">
                            <div class="order-product-title">薇诺娜修护精华液 敏感肌专用 保湿补水</div>
                            <div class="order-product-spec">规格：50ml</div>
                            <div class="order-product-price">¥198.00 × 1</div>
                        </div>
                    </div>
                    <div class="order-product">
                        <img src="https://images.unsplash.com/photo-1523275335684-37898b6baf30?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=300&q=80"
                            alt="智能手表">
                        <div class="order-product-info">
                            <div class="order-product-title">男士时尚腕表 商务休闲 防水夜光</div>
                            <div class="order-product-spec">颜色：银色</div>
                            <div class="order-product-price">¥1299.00 × 1</div>
                        </div>
                    </div>
                </div>
                <div class="order-footer">
                    <div class="order-total">实付款：<span>¥1497.00</span></div>
                    <div class="order-actions">
                        <button class="btn-delete">删除订单</button>
                    </div>
                </div>
            </div>

            <div class="order-item">
                <div class="order-header">
                    <div class="order-info">
                        <div class="order-id">订单号：20230620152436</div>
                        <div>下单时间：2023-06-20 15:24:36</div>
                    </div>
                </div>
                <div class="order-content">
                    <div class="order-product">
                        <img src="https://images.unsplash.com/photo-1572569511254-d8f925fe2cbb?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=300&q=80"
                            alt="蓝牙耳机">
                        <div class="order-product-info">
                            <div class="order-product-title">真无线蓝牙耳机 主动降噪 超长续航</div>
                            <div class="order-product-spec">颜色：白色</div>
                            <div class="order-product-price">¥399.00 × 2</div>
                        </div>
                    </div>
                </div>
                <div class="order-footer">
                    <div class="order-total">实付款：<span>¥798.00</span></div>
                    <div class="order-actions">
                        <button class="btn-delete">删除订单</button>
                    </div>
                </div>
            </div>

            <div class="order-item">
                <div class="order-header">
                    <div class="order-info">
                        <div class="order-id">订单号：20230618115642</div>
                        <div>下单时间：2023-06-18 11:56:42</div>
                    </div>
                </div>
                <div class="order-content">
                    <div class="order-product">
                        <img src="https://images.unsplash.com/photo-1546868871-7041f2a55e12?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=300&q=80"
                            alt="手表">
                        <div class="order-product-info">
                            <div class="order-product-title">女士时尚腕表 优雅设计 防水功能</div>
                            <div class="order-product-spec">颜色：玫瑰金</div>
                            <div class="order-product-price">¥899.00 × 1</div>
                        </div>
                    </div>
                </div>
                <div class="order-footer">
                    <div class="order-total">实付款：<span>¥899.00</span></div>
                    <div class="order-actions">
                        <button class="btn-delete">删除订单</button>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <script src="layui-v2.9.20/layui/layui.js"></script>
    <script>
        function getBackendBaseUrl() {
            // 根据当前环境确定后端URL
            if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
                return 'http://localhost:8080';
            }
            // 生产环境使用相同域名
            return window.location.origin;
        }
        function formatDateTime(dateStr) {
    if (!dateStr) return '';
    const date = new Date(dateStr);
    const y = date.getFullYear();
    const m = String(date.getMonth() + 1).padStart(2, '0');
    const d = String(date.getDate()).padStart(2, '0');
    const h = String(date.getHours()).padStart(2, '0');
    const min = String(date.getMinutes()).padStart(2, '0');
    return `${y}-${m}-${d} ${h}:${min}`;
}
        function getCurrentUserId() {
            // const user = sessionStorage.getItem('user');
            const user = JSON.parse(sessionStorage.getItem('user'));
            if (user) {
                try {
                    // const user = JSON.parse(user);
                    if (user && user.userId) {
                        console.log('当前用户ID:', user.userId);
                        return user.userId;
                    }
                } catch (e) {
                    console.error('解析用户信息失败', e);
                }
            }
            return null;
        }
        function renderOrderList(orders) {
    const $orderContainer = $('.order-container');
    $orderContainer.empty();

    if (!orders || orders.length === 0) {
        $orderContainer.html('<div class="no-orders">暂无订单</div>');
        return;
    }

    orders.forEach(order => {
        // 1. 合并相同 productId 的商品
        const mergedMap = {};
        if (order.orderItems && order.orderItems.length > 0) {
            order.orderItems.forEach(item => {
                const pid = item.productId;
                if (!mergedMap[pid]) {
                    mergedMap[pid] = { ...item };
                } else {
                    mergedMap[pid].quantity += item.quantity;
                }
            });
        }

        // 2. 渲染合并后的商品
        const mergedItems = Object.values(mergedMap);

        // 3. 计算实付款
        let totalPrice = 0;
        mergedItems.forEach(item => {
            // 确保 price 是数字
            const price = typeof item.price === 'number' ? item.price : parseFloat(item.price) || 0;
            totalPrice += price * item.quantity;
        });

        let productsHtml = '';
        if (mergedItems.length > 0) {
            productsHtml = mergedItems.map(item => `
                <div class="order-product">
                    <div class="order-product-info">
                        <img src="${item.productImage || ''}" alt="${item.productTitle || ''}">
                        <div class="order-product-title">${item.productTitle || ''}</div>
                        <div class="order-product-price">¥${item.price} × ${item.quantity}</div>
                    </div>
                </div>
            `).join('');
        } else {
            productsHtml = '<div class="order-product">无商品信息</div>';
        }

        const orderHtml = `
            <div class="order-item" data-order-id="${order.id}">
                <div class="order-header">
                    <div class="order-info">
                        <div class="order-id">订单号：${order.orderNo || order.id}</div>
                        <div>下单时间：${formatDateTime(order.createTime)}</div>
                    </div>
                </div>
                <div class="order-content">
                    ${productsHtml}
                </div>
                <div class="order-footer">
                    <div class="order-total">实付款：<span>¥${totalPrice.toFixed(2)}</span></div>
                </div>
            </div>
        `;
        $orderContainer.append(orderHtml);
    });
}
        function getOrderList() {
            // 从cookie或本地存储获取userId
            const userId = getCurrentUserId(); // 你需要实现这个函数

            if (!userId) {
                showStatusMessage('请先登录', false);
                // 可以跳转到登录页
                window.location.href = '/login';
                return;
            }

            $.ajax({
                url: getBackendBaseUrl() + "/order/list",
                method: 'GET',
                contentType: 'application/json',
                xhrFields: { withCredentials: true },
                headers: {
                    'userId': userId
                },
                success: function (data) {
    

                    if (data.code === 200) {
                        console.log('订单数据:', data.data);
                        renderOrderList(data.data);
                    } else {
                        // 请求失败
                        showStatusMessage('获取订单失败: ' + data.message, false);
                    }
                },
                error: function (xhr, status, error) {
                    console.error('请求失败:', error);
                    showStatusMessage('网络错误，请稍后重试', false);
                }
            });
        }
        
        document.addEventListener('DOMContentLoaded', function () {
            getOrderList();
            getCurrentUserId()
        });
    </script>
</body>

</html>